package com.game_info.controller;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.List;

import com.game_info.service.AdminService;
import com.game_info.utils.AjaxResult;
import com.game_info.vo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * admin表Controller
 *
 * @author ttj
 * @date 2020-04-14
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;


    /**
     * 查询管理员列表
     *
     * @param admin
     * @return 集合
     */
    @PostMapping("/list.do")
    @ResponseBody
    public AjaxResult list(Admin admin) {
        List<Admin> list = adminService.selectAdminList(admin);
        return AjaxResult.put(list, "查询管理员列表方法执行成功", list.size());
    }

    /**
     * @param admin
     * @return data:加密后的用户名,msg:登陆状态
     *   管理员登陆
     */
    @PostMapping("/adminLogin.do")
    @ResponseBody
    public AjaxResult adminLogin(@RequestBody Admin admin) throws UnsupportedEncodingException {
        if (admin.getaName() != null && admin.getaName() != "" && admin.getaPwd() != null && admin.getaPwd() != "") {
            Admin resAdmin = adminService.selectAdminByAdmin(admin);
            if (resAdmin != null && !resAdmin.equals("")) {
                // BASE64加密
                Base64.Encoder encoder = Base64.getEncoder();
                byte[] data = encoder.encode(("Logined:" + admin.getaName()).getBytes("UTF-8"));
                resAdmin.setaName(new String(data));//将加密用户名存入对象
                resAdmin.setaPwd(null);//清空查询到的密码
                System.out.println(resAdmin.toString());
                return AjaxResult.put(resAdmin, "登陆成功");
            } else {
                return AjaxResult.put(resAdmin, "登陆失败,用户名或密码输入错误");
            }
        }
        return AjaxResult.put(null, "用户名或密码不能为空");
    }

    /**
     * 根据id,用户名密码任意组合查询
     * @param admin
     * @return
     */
    @PostMapping("/selectAdminByAdmin.do")
    @ResponseBody
    public AjaxResult selectAdminByAdmin(@RequestBody Admin admin){
        Admin resAdmin=adminService.selectAdminByAdmin(admin);
        if (resAdmin!=null&&!resAdmin.equals("")){//不为空
            return AjaxResult.put(1,resAdmin,"查询成功",0);
        }else {
            return AjaxResult.put(0,resAdmin,"查询失败",0);
        }
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Admin admin) {
        return AjaxResult.put(adminService.insertAdmin(admin), "新增管理员方法执行成功");
    }

    /**
     * 修改管理员信息
     */
    @PostMapping("/edit.do")
    @ResponseBody
    public AjaxResult editSave(@RequestBody Admin admin) {
        return AjaxResult.put(adminService.updateAdmin(admin), "修改管理员方法执行成功");
    }

    /**
     * 批量删除
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return AjaxResult.put(adminService.deleteAdminByIds(ids), "批量删除管理员方法执行成功");
    }
}
